package com.zxgj.datadirect.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zxgj.common.annotation.Log;
import com.zxgj.common.core.controller.BaseController;
import com.zxgj.common.core.domain.AjaxResult;
import com.zxgj.common.core.page.TableDataInfo;
import com.zxgj.common.enums.BusinessType;
import com.zxgj.common.utils.poi.ExcelUtil;
import com.zxgj.datadirect.domain.DataDirectExchangeTask;
import com.zxgj.datadirect.dto.ExchangeTaskCreateReq;
import com.zxgj.datadirect.dto.ExchangeTaskLogResp;
import com.zxgj.datadirect.service.DataDirectExchangeTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/datadirect/exchangeTask")
@RequiredArgsConstructor
public class DataDirectExchangeTaskController extends BaseController {

    private final DataDirectExchangeTaskService exchangeTaskService;

    // ========== 创建任务 ========== 
    @PreAuthorize("@ss.hasPermi('datadirect:exchangeTask:create')")
    @Log(title = "交换任务创建", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult createTask(@RequestBody ExchangeTaskCreateReq req) {
        Long taskId = exchangeTaskService.createExchangeTask(req);
        return AjaxResult.success("交换任务创建成功", taskId);
    }

    // ========== 控制任务（启动/停止/修改） ========== 
    @PreAuthorize("@ss.hasPermi('datadirect:exchangeTask:control')")
    @Log(title = "交换任务控制", businessType = BusinessType.UPDATE)
    @PostMapping("/control")
    public AjaxResult controlTask(
        @RequestParam Long taskId,
        @RequestParam Integer operateType,
        @RequestBody(required = false) ExchangeTaskCreateReq req
    ) {
        boolean result = exchangeTaskService.controlExchangeTask(taskId, operateType, req);
        
        // 兼容 Java 8 的 switch 写法
        String operateMsg;
        switch (operateType) {
            case 1: operateMsg = "启动"; break;
            case 2: operateMsg = "停止"; break;
            case 3: operateMsg = "修改"; break;
            default: operateMsg = "操作"; break;
        }
        
        return result 
            ? AjaxResult.success(operateMsg + "成功") 
            : AjaxResult.error(operateMsg + "失败");
    }

    // ========== 查询任务列表 ========== 
    @PreAuthorize("@ss.hasPermi('datadirect:exchangeTask:list')")
    @GetMapping("/list")
    public TableDataInfo listTask(Integer taskStatus) {
        LambdaQueryWrapper<DataDirectExchangeTask> wrapper = new LambdaQueryWrapper<>();
        if (taskStatus != null) {
            wrapper.eq(DataDirectExchangeTask::getTaskStatus, taskStatus);
        }
        List<DataDirectExchangeTask> list = exchangeTaskService.list(wrapper);
        return getDataTable(list);
    }

    // ========== 查询任务日志 ========== 
    @PreAuthorize("@ss.hasPermi('datadirect:exchangeTask:log')")
    @GetMapping("/log")
    public TableDataInfo listTaskLog(
        @RequestParam Long taskId,
        @RequestParam(required = false) String startTime,
        @RequestParam(required = false) String endTime
    ) {
        List<ExchangeTaskLogResp> logList = exchangeTaskService.getExchangeTaskLogs(taskId, startTime, endTime);
        return getDataTable(logList);
    }

    // ========== 导出任务（Excel） ========== 
    @PreAuthorize("@ss.hasPermi('datadirect:exchangeTask:export')")
    @Log(title = "交换任务导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void exportTask(HttpServletResponse resp, Integer taskStatus) {
        LambdaQueryWrapper<DataDirectExchangeTask> wrapper = new LambdaQueryWrapper<>();
        if (taskStatus != null) {
            wrapper.eq(DataDirectExchangeTask::getTaskStatus, taskStatus);
        }
        List<DataDirectExchangeTask> list = exchangeTaskService.list(wrapper);
        
        ExcelUtil<DataDirectExchangeTask> util = new ExcelUtil<>(DataDirectExchangeTask.class);
        util.exportExcel(resp, list, "数据交换任务列表");
    }
}